<template>
  <el-row>
    <el-col :span="12">
      <div class="title-left">
        <img src="../assets/dishui02.png">
      </div>
    </el-col>
    <el-col :span="12">
      <div class="title-rigth"><img class="login"
             src="../assets/head1.gif"></div>
      <el-form :model="ruleForm"
               status-icon
               :rules="rules"
               ref="ruleForm"
               label-width="10"
               class="demo-ruleForm">
        <div class="myInput username1">
          <el-form-item prop="tel">
            <el-input class="myInput1 myInput2"
                      prefix-icon="el-icon-user"
                      v-model="ruleForm.username"
                      placeholder="请输入用户名"
                      clearable></el-input>
          </el-form-item>
        </div>
        <div class="myInput password">
          <el-form-item prop="pass">
            <el-input class="myInput1 myInput2"
                      prefix-icon="el-icon-magic-stick"
                      placeholder="请输入密码"
                      v-model="ruleForm.password"
                      show-password></el-input>
          </el-form-item>
        </div>
        <div class="login">
          <el-form-item>
            <el-button class="logBtn"
                       @click="submitForm('ruleForm')">登录</el-button>
          </el-form-item>
        </div>
      </el-form>
      
    </el-col>
  </el-row>

</template> 

<script>
import axios from 'axios'
import myapi from '../api.js'
export default {
  name: 'login',
  data() {
    // <!--验证手机号是否合法-->
    let validateName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('用户名不能为空！ '))
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }
    // <!--验证密码-->
    let validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error(' '))
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }
    return {
      user: [],
      ruleForm: {
        password: '',
        username: '',
      },
      rules: {
        password: [
          {
            validator: validatePass,
            trigger: 'change',
          },
        ],
        username: [
          {
            validator: validateName,
            trigger: 'change',
          },
        ],
      },
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios
            .post(myapi.Administrators.url, {
              username: this.ruleForm.username,
              password: this.ruleForm.password,
            })
            .then((res) => {
              console.log(res)
              if (res.data.code === 200) {
                this.user = res.data
                console.log(this.user)
                console.log('登录成功')
                this.$message({
                  showClose: true,
                  message: '欢迎回来，管理员！',
                  type: 'success',
                  center: true,
                })
                // this.$cookies.set()
                this.$store.state.username='管理员'
                this.$store.state.static="1"
                this.$cookies.set('status', 'logined')
                this.$cookies.set('id', this.ruleForm.username)
                this.$router.push({
                  path: '/index',
                })
              } else {
                console.log('登录失败')
                this.$message({
                  showClose: true,
                  message: '登录失败！请稍后重试！',
                  type: 'error',
                  center: true,
                })
              }
            })
        } else {
          console.log('账号或密码错误！')
          return false
        }
      })
    },
   
  },
}
</script>

<style>
.logo {
  margin: 0 auto;
}

.loginImg {
  width: 700px;
  margin: 0 auto;
  float: right;
}

/* ------------------------------ */

.title-left {
  margin-top: 5%;
  width: 40%;
  text-align: center;
  float: left;
  margin-right: 250px;
}
.title-rigth {
  margin-top: 5%;
  width: 40%;
  text-align: center;
  float: right;
  margin-right: 250px;
}

.head {
  margin-top: 10%;
  width: 450px;
  float: left;
}
.login {
  width: 350px;
}

.username1 {
  width: 40%;
  margin-top: 10%;
  margin-left: auto;
  margin-right: auto;
}

.password {
  width: 40%;
  margin-top: 30px;
  margin-left: auto;
  margin-right: auto;
}

.login {
  margin-top: 8%;
  margin-left: auto;
  margin-right: auto;
  text-align: center;
}

.logBtn {
  width: 150px;
  height: 40px;
  border-radius: 25px;
  border: none;
  outline: none;
  background-color: #4f6e9d;
  color: #ffffff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
}

.logBtn:focus,
.logBtn:hover {
  color: #ffffff;
  border-color: #7e9dca;
  background-color: #7e9dca;
}

.logBtn:active {
  color: #ffffff;
  border-color: #7e9dca;
  outline: 0;
}

.link {
  margin-top: 10%;
  margin-left: auto;
  margin-right: auto;
  text-align: center;
}
</style>